﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;


namespace Baosight.ZNH.ZMCM.UI
{
    /// <summary>
    /// 材料费用额度管理-树形结构 
    /// </summary>
    /// Title:ZMCM40，ServiceZMCM40
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 李红星
    /// Version: 1.0
    /// History:
    ///       2011-6-20  李红星   [创建]
    ///
    public partial class ZMCM40 : EFViewCmes
    {
        private EFTreeItem item;    //记录选中的树节点
        public ZMCM40()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ZMCM40_Loaded);
        }

        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ZMCM40_Loaded(object sender, RoutedEventArgs e)
        {
            this.DepartmentTree.Refresh();
        }

        /// <summary>
        /// 选中树节点，导航右侧的页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DepartmentTree_Selected(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            item = this.DepartmentTree.SelectedItems[0] as EFTreeItem;
            Dictionary<string, string> args = getTreeNodeInfo(item);
            if (item.IsLeaf == true)
            {
                this.container.LoadView<EFCustomViewContainerPanel>("ZMCM41", args);
            }
            //else
            //{
            //    //this.container.LoadView<EFCustomViewContainerPanel>("ZMCS03", args);
            //}
        }

        /// <summary>
        /// 获取tree选中的结点对应的label
        /// </summary>
        /// <param name="tree"></param>
        /// <returns></returns>
        private Dictionary<string, string> getTreeNodeInfo(EFTreeItem item)
        {
            string fdDepartmentId = item.ItemObject.GetType().GetProperty("label").GetValue(item.ItemObject, null) as string;
            string deptName = item.ItemObject.GetType().GetProperty("text").GetValue(item.ItemObject, null) as string;
            //string fdParentLevel = item.ItemObject.GetType().GetProperty("fdModuleLevel").GetValue(item.ItemObject, null) as string;
            string isLeaf = (item.IsLeaf == true) ? "1" : "0";

            Dictionary<string, string> args = new Dictionary<string, string>();
            args.Add("fdDepartmentId", fdDepartmentId);
            args.Add("isLeaf", isLeaf);
            //if (item.IsLeaf == false)
            //{
                args.Add("deptName", deptName);
                //args.Add("fdParentLevel", fdParentLevel);
            //}

            return args;
        }

        /// <summary>
        /// 重新基类的发布数据方法
        /// 刷新树节点
        /// </summary>
        /// <param name="data"></param>
        public override void OnEIDataPublished(Platform.Core.EIInfo data)
        {
            Dictionary<string, string> args = new Dictionary<string, string>();
            string _value = "";
            if (item != null)
            {
                if (item.IsLeaf)
                {
                    _value = (item.Parent as EFTreeItem).ItemObject.GetType().GetProperty("label").GetValue((item.Parent as EFTreeItem).ItemObject, null) as string;
                    args.Add("node", _value);
                }
                else
                {
                    _value = item.ItemObject.GetType().GetProperty("label").GetValue(item.ItemObject, null) as string;
                    args.Add("node", _value);
                }
            }
            BaseTools.RefreshTree("ZMCM40", "query", args, OnDoWorkCompleted);
        }

        /// <summary>
        /// OnEIDataPublished回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            this.DepartmentTree.EIDataContext = new Platform.Core.UI.EIDataContext();
            this.DepartmentTree.EIDataContext.Update(e.EIInfo);
            item.IsLoadOnDemandEnabled = true;
            if (item.IsLeaf)
            {
                EFTreeItem parent = item.Parent as EFTreeItem;
                string node = item.ItemObject.GetType().GetProperty("label").GetValue(item.ItemObject, null) as string;
                parent.Items.Clear();
                parent.CollapseItem();
                parent.ExpandItem();
                item = BaseTools.FindTreeItem(parent.Items, node, "label") as EFTreeItem;
                this.DepartmentTree.SelectedItem = item;
                return;
            }
            item.Items.Clear();
            item.CollapseItem();
            item.ExpandItem();
        }
        /// <summary>
        /// 第一次进入页面时选中树的第一个子节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DepartmentTree_ItemPrepared(object sender, Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs e)
        {
            if (e.PreparedItem == null)
            {
                return;
            }
            EFTreeItem treeItem = e.PreparedItem as EFTreeItem;
            treeItem.ExpandItem();//展开
            if (treeItem.Level == 1 && treeItem.Index == 0)//选中树的第一个子节点
            {
                this.DepartmentTree.SelectedItem = treeItem;//选中
            }
        }

       
    }
}
